#include "appengine.h"

AppEngine *AppEngine::m_instance = nullptr;
QMutex AppEngine::m_mutex;

AppEngine::AppEngine(QObject *parent)
    : QQmlApplicationEngine{parent}
{
    m_Config = LocalConfig::getInstance(this);
    m_Language = new LanguageControl(this);
    m_NetWorkManager = NetWorkManager::getInstance(this);

    m_Modbus = ModbusControl::getInstance(this);
    m_Printer = PrinterControl::getInstance(this);
    m_Scanner = ScannerControl::getInstance(this);

    m_Modbus->setModbusConnectParameter(m_Config->plc_Port(),
                                        m_Config->plc_Baudrate(),
                                        m_Config->plc_Databit(),
                                        m_Config->plc_Parity(),
                                        m_Config->plc_Stopbit());

    m_Modbus->connectToDevice();

    m_Printer->setConnectParameter(m_Config->printer_Port(),
                                   m_Config->printer_Baudrate(),
                                   m_Config->printer_Databit(),
                                   m_Config->printer_Parity(),
                                   m_Config->printer_Stopbit());

    m_Printer->connectToDevice();

    m_Scanner->setScannerConnectParameter(m_Config->scanner_Port(),
                                          m_Config->scanner_Baudrate(),
                                          m_Config->scanner_Databit(),
                                          m_Config->scanner_Parity(),
                                          m_Config->scanner_Stopbit());

    m_Scanner->connectToDevice();

    m_Printer->setPrinterType(m_Config->CurrentPrinter());
    m_Printer->setCurrentLanguage(m_Config->CurrentLanguage());

    m_Scanner->setCurrent_QR_Type(m_Config->Current_QR_Code());


    m_Language->setCurrentLanguage(m_Config->CurrentLanguage());
    m_Language->load_Language(m_Language->CurrentLanguage());

    // USB料单单例对象
    m_USB_Process_List_Model = USB_Process_List_Model::getInstance(this);

    // 网络料单单例对象
    m_Online_Process_List_Model = Online_Process_List_Model::getInstance(this);

    // 本地料单单例对象
    m_Order_Process_Manager = Order_Process_Manager::getInstance(this);
    m_Order_Process_Manager->isExistTable();

    registerToQML();

    // PLC与打印机  自动打印槽函数
    QObject::connect(m_Modbus,
                     &ModbusControl::send_Auto_Printer_Message_Signal,
                     m_Printer,
                     &PrinterControl::recv_Auto_Printer_Message_Slots);

    // 扫码枪与PLC 二维码料单交互
    QObject::connect(m_Scanner,
                     &ScannerControl::send_Scanner_Result_OutLine_Signal,
                     m_Modbus,
                     &ModbusControl::on_Scanner_Read_Result_Slots);

    // 网络料单与扫码枪 绑定 长风在线料单
    QObject::connect(m_Scanner,
                     &ScannerControl::send_Scanner_Result_ChangFeng_Signal,
                     m_NetWorkManager,
                     &NetWorkManager::recv_Scanner_Result_ChangFeng_Slots);
    // 网络料单与扫码枪 绑定 新格尔在线料单
    QObject::connect(m_Scanner,
                     &ScannerControl::send_Scanner_Result_XinGeEr_Signal,
                     m_NetWorkManager,
                     &NetWorkManager::recv_Scanner_Result_XinGeEr_Slots);
    // 网络料单与扫码枪 绑定 速制在线料单
    QObject::connect(m_Scanner,
                     &ScannerControl::send_Scanner_Result_SuZhi_Signal,
                     m_NetWorkManager,
                     &NetWorkManager::recv_Scanner_Result_SuZhi_Slots);
    // 网络料单与扫码枪 绑定 创品在线料单
    QObject::connect(m_Scanner,
                     &ScannerControl::send_Scanner_Result_ChuangPin_Signal,
                     m_NetWorkManager,
                     &NetWorkManager::recv_Scanner_Result_ChuangPin_Slots);
}

AppEngine::~AppEngine()
{
    m_Config->deleteLater();    
}

AppEngine *AppEngine::getInstance()
{
    if (!m_instance) {
        QMutexLocker locker(&m_mutex);
        if (!m_instance) {
            m_instance = new AppEngine();
        }
    }
    return m_instance;
}

void AppEngine::registerToQML()
{
    this->rootContext()->setContextProperty("Cpp_Config", m_Config);

    // 语言控制类
    this->rootContext()->setContextProperty("Cpp_Language", m_Language);
    // // 网络管理类
    // this->rootContext()->setContextProperty("Cpp_NetWork", m_NetWorkManager);

    // PLC控制类
    this->rootContext()->setContextProperty("Cpp_PLC", m_Modbus);
    // 打印机控制类
    this->rootContext()->setContextProperty("Cpp_Printer", m_Printer);
    // 扫码枪控制类
    this->rootContext()->setContextProperty("Cpp_Scanner", m_Scanner);


    // U盘料单数据
    this->rootContext()->setContextProperty("usbCutDataModel",m_USB_Process_List_Model);
    // 网络料单数据
    this->rootContext()->setContextProperty("onlineCutDataModel", m_Online_Process_List_Model);
    // 本地料单数据
    this->rootContext()->setContextProperty("Cpp_OrderProcess", m_Order_Process_Manager);


}

void AppEngine::destroyInstance()
{
    // 若实例存在，则删除（触发析构函数）
    if (m_instance != nullptr) {
        delete m_instance;    // 调用 ~AppEngine()
        m_instance = nullptr; // 置空，避免重复删除
    }
}
